﻿#region License
/*
 * Copyright ©2011-2012 EasyMon Project
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *  
 */
#endregion

using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using EasyMon.Common;
using EasyMon.Common.Configuration;
using EasyMon.Common.Sources;
using EasyMon.Server.Utils;

namespace EasyMon.Server
{
	/// <summary>
	/// This is the class that should contain all the code from Global.asax file, so that we can unit test the code.
	/// </summary>
	public class GlobalDependency
	{
        public const string ProductsConfigFileNameAppSetting = "ProductsConfigFileName";

		private readonly IAreaRegistrationFacade _areaRegistrationFacade;
		private readonly IGlobalFiltersFacade _globalFiltersFacade;
		private readonly IRouteTableFacade _routeTableFacade;

        static GlobalDependency()
        {
            DefaultConfigurationManager.Default = new DefaultWebConfigurationManager();
        }

		public GlobalDependency(IAreaRegistrationFacade areaRegistrationFacade, IGlobalFiltersFacade globalFiltersFacade, IRouteTableFacade routeTableFacade)
		{
			_areaRegistrationFacade = areaRegistrationFacade;
			_globalFiltersFacade = globalFiltersFacade;
			_routeTableFacade = routeTableFacade;
		}

		public GlobalDependency()
		{
			_routeTableFacade = new RouteTableFacade();
			_globalFiltersFacade = new GlobalFiltersFacade();
			_areaRegistrationFacade = new AreaRegistrationFacade();
		}

		public void Application_Start()
		{
			_areaRegistrationFacade.RegisterAllAreas();

			RegisterGlobalFilters(_globalFiltersFacade.GetFilters());
			RegisterRoutes(_routeTableFacade.GetRoutes());

            SystemStatus.Start(LoadSourceConfiguration());
		}

		private static void RegisterGlobalFilters(GlobalFilterCollection filters)
		{
			filters.Add(new HandleErrorAttribute());
		}

		private static void RegisterRoutes(RouteCollection routes)
		{
			routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

			routes.MapRoute(
				"Default", // Route name
				"{controller}/{action}/{id}", // URL with parameters
				new { controller = "Dashboard", action = "Dashboard", id = UrlParameter.Optional } // Parameter defaults
			);
		}

        private static SourceRoot LoadSourceConfiguration()
        {
            var fileName = DefaultConfigurationManager.Default.GetAppSetting(ProductsConfigFileNameAppSetting);

            return SourceRoot.Load(HttpContext.Current.Server.MapPath(string.Format("~/{0}", fileName)));
        }
	}
}